home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / VirtualLight / VLight1.3win32.exe / VibSDK / Samples / sample4.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-07  |  2.9 KB  |  114 lines

  1. /*
  2.  * VirtuaLight's binary .VIB format API, sample 4
  3.  * Written by Stephane Marty, 09/10/2001
  4.  *
  5.  * This sample program writes a binary VirtuaLight MSH file
  6.  * describing a procedural spring mesh.
  7.  */
  8.  
  9. #include "..\vlBinDef.h"
  10.  
  11. #define TWO_PI (6.283185307179586476925287)
  12.  
  13. #define NU 180
  14. #define NV 24
  15. #define R1 0.5
  16. #define R2 0.2
  17. #define PERIOD 2
  18. #define CYCLES 5
  19.  
  20. static viVECTOR
  21. normal(viVECTOR v, viVECTOR v1, viVECTOR v2)
  22. {
  23.     viVECTOR n, va, vb;
  24.  
  25.     viVecSubstract(va, v1, v);
  26.     viVecNormalize(va);
  27.     viVecSubstract(vb, v2, v);
  28.     viVecNormalize(vb);
  29.     viVecCross(n, va, vb);
  30.     viVecNormalize(n);
  31.     return(n);
  32. }
  33.  
  34. static viVECTOR
  35. evaluate(double u, double v)
  36. {
  37.     viVECTOR q;
  38.  
  39.     viSetDbl(q.x, (1.0 - R1 * cos(v)) * cos(u));
  40.     viSetDbl(q.y, (1.0 - R1 * cos(v)) * sin(u));
  41.     viSetDbl(q.z, R2 * (sin(v) + u * PERIOD / M_PI));
  42.     return(q);
  43. }
  44.  
  45. void main(void)
  46. {
  47.     unsigned long pri=0;
  48.     int i, j;
  49.     double u, v, du, dv;
  50.     viVECTOR q[4], n[4], vec;
  51.     viPATCH            tri;
  52.    
  53.     du = CYCLES * TWO_PI / (double)NU;
  54.     dv = TWO_PI / (double)NV;
  55.  
  56.     // Create a new MSH file
  57.     if(viNewMSHFile("sample4.msh") != TRUE)
  58.     {
  59.         fprintf(stderr, "\nImpossible to create the file.\n");
  60.         exit(-1);
  61.     }
  62.  
  63.     // Create and declare the procedural object named "smooth_spring"
  64.     viStartMeshDeclaration("smooth_spring");
  65.     for (i=0; i<NU; i++)
  66.     {
  67.         u = i * du;
  68.         for (j=0; j<NV; j++)
  69.         {
  70.             v = j * dv;
  71.             // this object is a set of polygons, but each of them
  72.             // is divided in two triangular patches.
  73.             q[0] = evaluate(u, v);
  74.             n[0] = normal(q[0], evaluate(u+du/10, v), evaluate(u, v+dv/10));
  75.             q[1] = evaluate(u+du, v);
  76.             n[1] = normal(q[1], evaluate(u+du+du/10, v), evaluate(u+du, v+dv/10));
  77.             q[2] = evaluate(u+du, v+dv);
  78.             n[2] = normal(q[2], evaluate(u+du+du/10, v+dv), evaluate(u+du, v+dv+dv/10));
  79.             q[3] = evaluate(u, v+dv);
  80.             n[3] = normal(q[3], evaluate(u+du/10, v+dv), evaluate(u, v+dv+dv/10));
  81.             // set and dump the first triangular patch...
  82.             viCopyVector(&tri.v1, &q[0]);
  83.             viCopyVector(&tri.n1, &n[0]);
  84.             viCopyVector(&tri.v2, &q[1]);
  85.             viCopyVector(&tri.n2, &n[1]);
  86.             viCopyVector(&tri.v3, &q[2]);
  87.             viCopyVector(&tri.n3, &n[2]);
  88.             viAddPatchToMesh(&tri);
  89.             // ...and the second one (first vertex doesn't change).
  90.             viCopyVector(&tri.v2, &q[2]);
  91.             viCopyVector(&tri.n2, &n[2]);
  92.             viCopyVector(&tri.v3, &q[3]);
  93.             viCopyVector(&tri.n3, &n[3]);
  94.             viAddPatchToMesh(&tri);
  95.             pri += 2;
  96.         }
  97.     }
  98.     viEndMeshDeclaration();
  99.  
  100.     // The following lines invoke (call) the mesh
  101.     // from inside the MSH file.
  102.     // If you rather prefer call it from the ASCII VIB
  103.     // scene, remove them and add the following to the VIB file:
  104.     // smooth_spring [ Rotate(0,0,45) smooth_spring_shader ]
  105.     viCallMesh("smooth_spring");
  106.     viSetMeshShader("smooth_spring_shader");
  107.     viRotateMesh(viSetVector(&vec, 0, 0, 45));
  108.     viEndMesh();
  109.  
  110.     // Close the MSH file
  111.     (void)viCloseMSHFile();
  112.  
  113.     fprintf(stderr, "\n%lu patches contained into the MSH file.\n", pri);
  114. }